/*
 * @Author: 陈 娇
 * @Date: 2022-02-21 10:39:23
 * @LastEditTime: 2022-02-23 10:08:49
 * @LastEditors: Please set LastEditors
 * @Description: 公共方法
 * @FilePath: \ebc-h5\utils\utils.js
 */
import JSEncrypt from 'jsencrypt'
import Encrypt from 'encryptlong'
 
export function asyncApiFun(apiFun, ...parasm) {
  return apiFun(...parasm).then(res => [res, null]).catch(err => [null, err])
}


/**
 * @param {string} str 手机号码
 * @returns {Boolean}
 */
export function validPhone(str) {
	const reg = /^1(3|4|5|6|7|8|9)\d{9}$/
	return reg.test(str)
}
// 多次跳转

export const throttle = (fn,gapTime)=>{
// console.log('gapTime')
	if (gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}
	let _lastTime = null
	return function () {
		const _nowTime = + new Date()
		console.log(_nowTime , _lastTime , gapTime , _lastTime)
		if (_nowTime - _lastTime > gapTime || !_lastTime) {
			console.log(_nowTime)
			_lastTime = _nowTime
			// 将this和参数传给原函数
			fn.apply(this,arguments)
		}
	}
}
const priKey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIv6MUgSamV67ORTayRDIAZZmR2Jvu8ua/Uc8zVDdxMNEh9uDXCX2S7UGt1EBn49D8HuH6HtT9zlxzuZMSwgTuruYRql0kdiJ03+h7VeG7iZaB/1dpkZwI8KsHA768CfyFShZg9sI7k6nRLTlMHCgg8EXAsRJeoio/4acmmjaJLXAgMBAAECgYACvYQneAzTLcPjeGqZjCLdXPWtaVvujMcaRlXQjiCPo+rd6KaktKRQ0WAxQrqp0AI19rZx0haFKY5r8UTQK1XAJjZdNtmeYQvIwY9fCtX+HND0G4+i1YbFCz2E3Z1U4eR5ndk+eeNG2s1lQxu6p9zyd6VEjDR9aqHH7b41DwV38QJBANBeyvTUsF779Pl23W1xBYHqqLlzaEZ37yc5sFqbTyWLILO/5QmgfvoSKvHsbQwNudLj/XLuTceNt12WR61YkJkCQQCr+T9Jd3VbuqbEynzpegYippWpAh09HWlqCS9kXhyhrNbhtHeT2K3D/Acbhg71L9PHK8sUVTeRUgYvKjB7PrTvAkALiCN9V0QHjbQqFjz88JNYdE8wLEXqGHsg1Z1/9jZ0InYF3KnlVX+RgW3lA6upTh0pobqGINAnKh2qzUVcP5mJAkEAhHnj079PS5NUZqIpaV3Tnhu/jEsRpQOkMcfTlzicS7suaP4xY1X2PdRG0WAK47utI2/L5+ThgnOySeDxKe+bRwJBAJ6T+QG/+VcDN5mAnSOqlyrdZ6sBfw8lmVMlRNACg2GYxA8ByTQB2pmQlWH1JBy0bE8fOImf4hrskitu1FTzCo4='
const pubKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL+jFIEmpleuzkU2skQyAGWZkdib7vLmv1HPM1Q3cTDRIfbg1wl9ku1BrdRAZ+PQ/B7h+h7U/c5cc7mTEsIE7q7mEapdJHYidN/oe1Xhu4mWgf9XaZGcCPCrBwO+vAn8hUoWYPbCO5Op0S05TBwoIPBFwLESXqIqP+GnJpo2iS1wIDAQAB'

export function rsaPublicData(str) {
	const encrypt = new JSEncrypt();
	encrypt.setPublicKey(pubKey);
	const encrypted = encrypt.encrypt(str);
	return encrypted
	// console.log('加密后数据:%o', encrypted);
}
export function rsaPrivateData(str) {
	const decrypt = new Encrypt();
	//使用私钥解密
	// const decrypt = new JSEncrypt();
	decrypt.setPrivateKey(priKey);
	const uncrypted = decrypt.decryptLong(str) // decrypt.decrypt(str);
	return uncrypted;
	// console.log('解密后数据:%o', uncrypted); // 张三

}